package org.sxm.cloud.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.sxm.cloud.client.PayClient;
import org.sxm.cloud.dao.OrderDao;
import org.sxm.cloud.dto.order.OrderDTO;
import org.sxm.cloud.dto.pay.PayDTO;
import org.sxm.cloud.entity.order.OrderEntity;
import org.sxm.cloud.entity.product.ProductEntity;
import org.sxm.cloud.exception.BizException;
import org.sxm.cloud.service.IOrderService;
import org.sxm.cloud.client.ProductClient;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

@Service("orderService")
public class OrderService implements IOrderService {

    private final static Logger log = LoggerFactory.getLogger(OrderService.class);

    @Autowired
    private OrderDao orderRepository;

    @Autowired
    private ProductClient productClient;

    @Autowired
    private PayClient payClient;

    @Override
    public OrderEntity getByOrderId(Integer id) {
        log.info("查询订单信息{}", id);
        OrderEntity order = orderRepository.findById(id).orElse(null);
        if(Objects.isNull(order)){
            throw new BizException("查询的订单信息不存在");
        }
        //补全商品信息
        ProductEntity productEntity = productClient.getProduct(order.getProductId());
        order.setProduct(productEntity);
        return order;
    }

    @Override
    @Transactional
    public int addOrder(OrderDTO order) {
        log.info("用户下单，订单信息{}", order);
        if (order.getProductNum() <= 0) {
            throw new RuntimeException("订单产品数量不能小于0");
        }
        //查询产品信息
        ProductEntity product = productClient.getProduct(order.getProductId());
        if (Objects.isNull(product)) {
            throw new BizException("产品信息不存在");
        }
        log.info("查询产品库存信息，产品服务返回{}", product);
        //保存订单
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderName(product.getName() + "订单");
        orderEntity.setStatus(1);//未支付状态
        orderEntity.setProductName(product.getName());
        orderEntity.setNum(order.getProductNum());
        orderEntity.setProductId(order.getProductId());
        orderEntity.setTotalPrice(product.getPrice().multiply(new BigDecimal(order.getProductNum())));
        orderEntity.setCreateTime(new Date());
        orderRepository.save(orderEntity);
        return 1;
    }

    @Override
    public int cancelOrder(Integer orderId) {
        return 0;
    }

    @Override
    @Transactional
    public int updateOrderPayStatus(Integer orderId) {
        OrderEntity order = this.getByOrderId(orderId);
        if (Objects.isNull(order)) {
            throw new BizException("订单信息不存在");
        }
        if (order.getStatus() != 1) {
            throw new BizException("订单状态非支付状态，不允许支付");
        }
        //设置订单状态
        order.setStatus(3); //设置状态已支付
        orderRepository.save(order);

        //调支付服务，进行用户支付入账
        PayDTO payDTO = new PayDTO();
        payDTO.setOrderId(orderId);
        payDTO.setPayPrice(order.getTotalPrice());
        payDTO.setPayUserId(2);
        payDTO.setPayeeUserId(13);
        payClient.pay(payDTO);

        //调产品服务，进行库存扣减
        productClient.reduseProduct(order.getProductId(),order.getNum());
        return 1;

    }
}
